package cn.school.controller.classManage;

import cn.school.classManage.service.SchoolClassService;
import cn.school.classManage.vo.ClassTeacherRelationVo;
import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.SysUserUtils;
import cn.school.course.service.CourseService;
import cn.school.educational.vo.SubjectExcelVo;
import cn.school.notice.entity.FamilyNotice;
import cn.school.sys.entity.SysUser;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/classInfo")
@Api(tags = "Basedata-bclass=> 班级管理")
public class SchoolClassController {
	@Autowired
	private SchoolClassService schoolClassService;
	
	@Autowired
	private CourseService courseService;
	
	@GetMapping("/excel/importTemplate")
	public void importTemplate(HttpServletResponse response) throws IOException {
		EasyExcel.write(response.getOutputStream(), SubjectExcelVo.class).head(getHeader()).sheet("班级信息表").doWrite(null);
	}
	
	private List<List<String>> getHeader() {
		String first = "班级信息导入";
		List<List<String>> list = new ArrayList<List<String>>();
		List<String> head0 = new ArrayList<String>();
		head0.add(first);
		head0.add("班级名称");
		List<String> head1 = new ArrayList<String>();
		head1.add(first);
		head1.add("年级");
		list.add(head0);
		list.add(head1);
		return list;
	}
	
	@PostMapping("/excel/importData")
	public CommonResult<String> importClassINfoExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException{
		try {
			return schoolClassService.importClassInfoExcel(multipartFile.getInputStream());
		} catch (ApiException apiException) {
			throw apiException;
		} catch (Exception e) {
			throw new ApiException("导入班级信息失败，请联系管理员");
		}
	}
	
	
	/**
	 * 分页查询班级列表
	 *
	 * @param vo
	 * @return
	 * @throws ApiException
	 * @throws IOException
	 */
	@PostMapping("/findSchoolClassPageList")
	@ApiOperation("findSchoolClassPageList => 分页查询班级列表")
//    @PreAuthorize("@ss.hasPermi('class:info:list')")
	public CommonResult<CommonPage<SchoolClassVO>> findSchoolClassPageList(@RequestBody SchoolClassVO vo) throws ApiException, IOException {
		IPage<SchoolClassVO> pageList = schoolClassService.findSchoolClassPageList(vo);
		return CommonResult.success(CommonPage.restPage(pageList));
	}
	
	/**
	 * 分页查询班主任班级
	 *
	 * @param vo
	 * @return
	 * @throws ApiException
	 * @throws IOException
	 */
	@PostMapping("/findTeacherClassPageList")
	@ApiOperation("findTeacherClassPageList => 分页查询班级列表")
	public CommonResult<CommonPage<SchoolClassVO>> findTeacherClassPageList(@RequestBody SchoolClassVO vo) throws ApiException, IOException {
		vo.setClassTeacherId(SysUserUtils.getSysUser().getUsername());
		vo.setLoginUserId(SysUserUtils.getSysUser().getId());
		IPage<SchoolClassVO> pageList = schoolClassService.findSchoolClassPageList(vo);
		return CommonResult.success(CommonPage.restPage(pageList));
	}
	
	
	/**
	 * 新增或修改班级信息
	 *
	 * @param vo
	 * @return
	 * @throws ApiException
	 * @throws IOException
	 */
	@PostMapping("/addOrUpdateSchoolClassInfo")
	@PreAuthorize("@ss.hasPermi('class:info:edit')")
	public CommonResult<Integer> addOrUpdateSchoolClassInfo(@RequestBody SchoolClassVO vo) throws ApiException, IOException {
		Integer num = schoolClassService.addOrUpdateSchoolClassInfo(vo);
		if (num > 0) {
			return CommonResult.success();
		}
		return CommonResult.failed();
	}
	
	/**
	 * 删除班级信息
	 *
	 * @param vo
	 * @return
	 * @throws ApiException
	 * @throws IOException
	 */
	@PostMapping("/deleteSchoolClassInfo")
	@PreAuthorize("@ss.hasPermi('class:info:del')")
	public CommonResult<Integer> deleteSchoolClassInfo(@RequestBody SchoolClassVO vo) throws ApiException, IOException {
		Integer num = schoolClassService.deleteSchoolClassInfo(vo.getId());
		if (num > 0) {
			return CommonResult.success();
		}
		return CommonResult.failed();
	}
	
	/**
	 * 查询所有班级
	 *
	 * @return
	 * @throws ApiException
	 */
	@GetMapping("/findAllSchoolClass")
	public CommonResult<List<SchoolClassVO>> findAllSchoolClass() throws ApiException {
		List<SchoolClassVO> all = schoolClassService.findAllSchoolClass();
		return CommonResult.success(all);
	}
	
	@GetMapping("/findClassByTeacher")
	public CommonResult<List<SchoolClassVO>> findClassByTeacher() throws ApiException {
		List<SchoolClassVO> all = schoolClassService.findClassByTeacher();
		return CommonResult.success(all);
	}
	
	@PostMapping("/batchUpdateClassStatus")
	@PreAuthorize("@ss.hasPermi('class:info:batchUpdate')")
	public CommonResult<String> batchUpdateClassStatus(@RequestBody List<SchoolClassVO> list) throws ApiException {
		if (list.isEmpty()) {
			return CommonResult.failed("缺少必要数据");
		}
		int num = schoolClassService.batchUpdateClassStatus(list);
		if (num > 0) {
			return CommonResult.success();
		}
		;
		return CommonResult.failed();
	}
	
	@ApiOperation("findAcsRecordByClassId => 通过班级id查看班级学生门禁记录通知")
	@PostMapping("/findAcsRecordByClassId")
	public CommonResult<CommonPage<FamilyNotice>> findAcsRecordByClassId(@RequestBody SchoolClassVO classVO) {
		CommonPage<FamilyNotice> result = schoolClassService.findAcsRecordByClassId(classVO);
		return CommonResult.success(result);
	}
	
	@ApiOperation("findAcsRecordByClassId => 按名称查询老师id")
	@PostMapping("/queryByStudentName")
	public CommonResult<SysUser> queryByStudentName(@RequestParam(value = "classId", required = true) Integer classId) {
		return CommonResult.success(this.schoolClassService.queryByStudentName(classId));
	}
	
	@GetMapping("/findClassInfoByClassId/{classId}")
	public CommonResult findClassInfoByClassId(@PathVariable("classId") Long classId) {
		SchoolClassVO schoolClassVO = schoolClassService.findClassInfoByClassId(classId);
		return CommonResult.success(schoolClassVO);
	}
	
	/**
	 * 教员配置
	 *
	 * @return
	 * @author alex
	 * @date 2023/8/2 9:11
	 */
	@PostMapping("/addTeacherRelation")
	public CommonResult<String> classTeacherConfiguration(@RequestBody List<ClassTeacherRelationVo> vos) {
		int num = schoolClassService.classTeacherConfig(vos);
		if (num== 1) {
			return CommonResult.failed("科目和教员重复");
		}
		if (num <= 0) {
			return CommonResult.failed();
		}
		return CommonResult.success();
	}
}
